from sqlalchemy.ext.asyncio import AsyncSession as Session
from app.schemas.uc.user import UserCreate
from app.models.uc.user import User
from sqlalchemy import select
from typing import Optional

async def create_user(session: Session, user: UserCreate):
    db_user = User.model_validate(user)
    session.add(db_user)
    await session.commit()
    await session.refresh(db_user)
    return db_user

async def get_user_by_mobile(session: Session, mobile: str) -> Optional[User]:
    result = await session.execute(select(User).filter(User.mobile == mobile))
    return result.scalar_one_or_none()

async def get_user_by_id(session: Session, uid: int) -> Optional[User]:
    """通过ID获取单个用户"""
    statement = select(User).where(User.uid == uid)
    result = await session.execute(statement)
    return result.scalar_one_or_none()
